<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
//require('echarts/theme/macarons'); // echarts theme
import resize from '../components/mixins/resize'

const animationDuration = 1000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '314px'
    },
    apiData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      chart: null,
      option: {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',
          textStyle: {
            color: '#fff',
            fontStyle: 'normal',
            fontFamily: '微软雅黑',
            fontSize: 12
          }
        },
        legend: {
          left: 'center',
          top: 'bottom',
          data: ['在邻站的作业时间', '非生产时间', '生产时间'],
          textStyle: {
            color: '#fff'
          }
        },
        color: ['#52BCA9', '#E46666', '#3BA272', '#FC8452', '#9A60B4', '#73C0DE', '#91CC75', '#5470C6'],
        series: [
          {
            name: '调小机车班产量',
            type: 'pie',
            radius: [50, 100],
            center: ['50%', '50%'],
            roseType: 'area',
            itemStyle: {
              borderRadius: 6
            },
            data: []
          }
        ]
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.option.series[0].data = []
      this.chart.setOption(this.option)
    },
    updateChart() {
      this.option.series[0].data[0] = { value: this.apiData.neighborWorkHour, name: '在邻站的作业时间' },
      this.option.series[0].data[1] = { value: this.apiData.notProductHour, name: '非生产时间' },
      this.option.series[0].data[2] = { value: this.apiData.produceHour, name: '生产时间' },
      this.chart.setOption(this.option)
    }
  }
}
</script>
